package com.vipheyue.campusrecyce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.siyamed.shapeimageview.CircularImageView;
import com.vipheyue.campusrecyce.R;
import com.vipheyue.campusrecyce.bean.MyUser;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class LoginActivity extends AppCompatActivity {

    @Bind(R.id.backgroundImage)
    ImageView backgroundImage;
    @Bind(R.id.login_2fa)
    LinearLayout login_2fa;
    @Bind(R.id.cirIV_userIcon)
    CircularImageView cirIV_userIcon;
    @Bind(R.id.et_phone)
    EditText et_phone;
    @Bind(R.id.et_password)
    EditText et_password;
    @Bind(R.id.editValify)
    EditText editValify;
    @Bind(R.id.imageValify)
    ImageView imageValify;
    @Bind(R.id.captchaLayout)
    RelativeLayout captchaLayout;
    @Bind(R.id.loginLayout)
    LinearLayout loginLayout;
    @Bind(R.id.edit2FA)
    EditText edit2FA;
    @Bind(R.id.layout2fa)
    LinearLayout layout2fa;
    @Bind(R.id.bt_login)
    Button bt_login;
    @Bind(R.id.tv_restPassword)
    TextView tv_restPassword;
    @Bind(R.id.tv_register)
    TextView tv_register;
    @Bind(R.id.layoutRoot)
    RelativeLayout layoutRoot;
    private int requestCode = 5555;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_login, R.id.tv_restPassword, R.id.tv_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                startLogin();
                break;
            case R.id.tv_restPassword:
                startMyIntent("忘记密码");
                break;
            case R.id.tv_register:
                startMyIntent("注册");
                break;
        }
    }

    private void startLogin() {
        BmobUser.loginByAccount(this, et_phone.getText().toString().trim(), et_password.getText().toString().trim(), new LogInListener<MyUser>() {

            @Override
            public void done(MyUser user, BmobException e) {
                // TODO Auto-generated method stub
                if(user!=null){
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    finish();
                }else{
                    Toast.makeText(LoginActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void startMyIntent(String type) {
        Intent forgetIntent = new Intent(this, GetSMSActivity.class);
        forgetIntent.putExtra("type", type);
        startActivityForResult(forgetIntent, requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != -1) {
            return;
        }
        String phone = data.getStringExtra("phone");
        String password = data.getStringExtra("password");
        if (!TextUtils.isEmpty(phone)) {
            et_phone.setText(phone);
        }
        if (!TextUtils.isEmpty(password)) {
            et_password.setText(password);
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
